package com.chalk.common.plugins.tencent;

import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author xiaoqiang
 * @Description 腾讯短信推送
 * @date 2019/6/10 17:19
 */
public class TencentSmsUtil {

    private static Logger logger  = LoggerFactory.getLogger(TencentSmsUtil.class);

    // 短信应用 SDK AppID
    public static int appid = 1400163511;

    // 短信应用 SDK AppKey
    public static String appkey = "b1b819d3d2ac30a8a7b76255d3cd4c22";

    // NOTE: 签名参数使用的是`签名内容`，而不是`签名ID`。这里的签名"腾讯云"只是示例，真实的签名需要在短信控制台申请
    public static String smsSign = "阿卢浮漂";


    /**
     * 指定模板 ID 单发短信
     * @param templateId
     * @param phoneNumber
     * @param params
     */
    public static void smsSingleSend(Integer templateId, String phoneNumber, String[] params) {
        SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
        // 签名参数未提供或者为空时，会使用默认签名发送短信
        SmsSingleSenderResult result = null;
        try {
            result = ssender.sendWithParam("86", phoneNumber, templateId, params, smsSign, "", "");
            logger.info("单发短息：" + result);
        } catch (HTTPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 指定模板 ID 群发短信
     * @param templateId
     * @param phoneNumbers
     * @param params
     */
    public static void smsMultiSend(Integer templateId, String[] phoneNumbers, String[] params){
        SmsMultiSender msender = new SmsMultiSender(appid, appkey);
        try {
            SmsMultiSenderResult result =  msender.sendWithParam("86", phoneNumbers,
                    templateId, params, smsSign, "", "");
            logger.info("群发短息：" + result);
        } catch (HTTPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   /* public static void main(String[] args) {
        Integer templateId = 233102;
        String phoneNumber = "18710966568";
        String[] params = {"小马哥,"};
        smsSingleSend(templateId,phoneNumber,params);
        }*/
        }
